class Controller:
    """ shared object in which the agents can add information
    about the world and the stuff they observe """


    ROLE_NONE = 0
    ROLE_CP1 = 1
    ROLE_CP2 = 2
    ROLE_HUNTER = 3

    starting_roles = [ROLE_CP1, ROLE_HUNTER, ROLE_CP2]

    def __init__(self, settings):
        # lists of the agents I recon we might want to use
        self.agents = []
        self.settings = settings
        #print """settings %s: max_turn %d, max_range: %d""" % (
        #    settings, settings.max_turn, settings.max_range)

        # list of ammo locations
        self.ammo_locations = [(152, 136), (312, 136)]

        # list of control points
        self.cps = [(216, 56), (248, 216)]

        # list of enemy locations and the time they were spotted
        # the list contains lists of [x, y, angle, steps_ago], with steps_ago
        # indicating how many steps have passed since the enemy was spotted
        # there
        self.enemies = []

        # time step to enemy locations and the like
        self.step = 1


        self.role_methods = {self.ROLE_NONE:self.role_none,
                                self.ROLE_HUNTER:self.role_hunter,
                                self.ROLE_CP1:self.role_cp,
                                self.ROLE_CP2:self.role_cp}



    def add_agent(self, agent):
        """ Agents should add themselves
        in the constructor so the id matches
        the index in the self.agents list"""
        self.agents.append(agent)
        agent.role = self.starting_roles[agent.id]


    def set_agent_goal(self, agent):
        """
        Specifies the decision tree of our robot, using the actions in
        WaardFunctionality

        Will contain 2 roles:
        - defender#: will defend cp # (as many defenders as cp's should always
          be available)
        - attacker: All other tanks are attackers
        """

        agent.role = self.get_role_for_agent(agent)
        self.role_methods[agent.role](agent)



    def role_cp_1(self, agent):
        self.func.set_goal_to_point(agent.id, agent.role[2])


    def role_hunter(self, agent):
        """ behavior for the hunter role """
        if(agent.observation.ammo > 0):
            print "ammo > 0"
            if not(self.func.set_goal_to_shoot(agent.id)):
                if(self.func.set_goal_to_finest_enemy(agent.id)):
                    return
        if not(self.func.set_goal_to_shoot(agent.id)):
            if not(self.func.set_goal_to_close_ammo(agent.id)):
                #if not(self.func.set_goal_to_control_point(agent.id)):
                self.func.set_goal_to_ammo(agent.id)

        if self.func.agents[agent.id].goal == None:
            print "falling back to set goal to ammo"
            self.func.set_goal_to_ammo(agent.id)
